package com.show.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.show.pojo.Trainee;
import com.show.service.TraineeService;
import com.show.service.impl.TraineeServiceImpl;

import java.util.LinkedList;
import java.util.List;

public class DemoDataListener extends AnalysisEventListener<Trainee> {
    private List<Trainee> list = new LinkedList<Trainee>();
    private TraineeService traineeService = new TraineeServiceImpl();

    /**
     * 这个每一条数据解析都会来调用
     * @param trainee
     * @param analysisContext
     */
    @Override
    public void invoke(Trainee trainee, AnalysisContext analysisContext) {
        list.add(trainee);
    }

    /**
     * 所有数据读取完了调用
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        String jsonString = JSON.toJSONString(list);
        List<Trainee> trainees = JSONObject.parseArray(jsonString.toString(), Trainee.class);
        Integer integer = traineeService.saveAll(trainees);
        System.out.println(integer);
    }
}
